home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / proff.zip / PROFF02.C < prev    next >
C/C++ Source or Header  |  1988-02-12  |  3KB  |  180 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "defs.h"
  4. #include "debug.h"
  5.  
  6. /*
  7.  * addset - put c in array[i], if it fits, increment i
  8.  *
  9.  */
  10. int
  11. addset(c,array,i,maxsize)
  12. char c;
  13. char array[];
  14. int *i;
  15. int maxsize;
  16. {
  17.     int n,status = NO;
  18.     dprintf("addset  ");
  19.     n = *i;
  20.     if (n <= maxsize - 1) {
  21.         array[n++] = c;
  22.         status = YES;
  23.     }
  24.     *i = n;
  25.     return(status);
  26. }
  27.  
  28. /*
  29.  * addstr - add string s to str[i] if it fits, increment i
  30.  *
  31.  */
  32. int
  33. addstr(s,str,i,maxsize)
  34. char s[];
  35. char str[];
  36. int *i;
  37. int maxsize;
  38. {
  39.     int k,j,status = NO;
  40.  
  41.     dprintf("addstr  ");
  42.     j = *i;
  43.     if (j + strlen(s) <= maxsize - 1) {
  44.         for (k = 0; s[k] != EOS; k++)
  45.             str[j++] = s[k];
  46.         status = YES;
  47.     }
  48.     *i = j;
  49.     return(status);
  50. }
  51.  
  52. /*
  53.  * ctoi - convert string at in[i] to integer, increment i
  54.  *
  55.  */
  56. int
  57. ctoi(in,i)
  58. char in[];
  59. int *i;
  60. {
  61.     int j,n,sign;
  62.  
  63.     dprintf("ctoi  ");
  64.     n = 0;
  65.     for (j = *i; in[j] == ' ' || in[j] == '\t'; j++)
  66.         ;         /* skip leading garbage */
  67.     sign = 1;
  68.     if (in[j] == '+' || in[j] == '-') /* sign */
  69.         sign = (in[j++] == '+') ? 1 : -1;
  70.     for (n = 0; in[j] >= '0' && in[j] <= '9'; j++)
  71.         n = 10 * n + in[j] - '0';
  72.     *i = j;
  73.     return(sign * n);
  74. }
  75.  
  76. /*
  77.  * error - print message and terminate
  78.  *
  79.  */
  80. error(s)
  81. char s[];
  82. {
  83.     fprintf(stderr,"%s\n",s);
  84.     exit(1);
  85. }
  86.  
  87. /*
  88.  * getwrd - get non-blank word from in[i] into out, increment i
  89.  *
  90.  */
  91. int
  92. getwrd(in,i,out)
  93. char in[];
  94. int *i;
  95. char out[];
  96. {
  97.     int j,size = 0;
  98.  
  99.     for (j = *i; in[j] == '\t' || in[j] == ' '; j++)
  100.         ;         /* skip leading garbage */
  101.     while (in[j] != ' ' && in[j] != '\t' && 
  102.         in[j] != EOS && in[j] != '\n')
  103.         out[size++] = in[j++];
  104.     out[size++] = EOS;
  105.     *i = j;
  106. #ifdef DEBUG
  107.     printf("getwrd: %s\n",out);
  108. #endif
  109.     return(size);
  110. }
  111. /*
  112.  * skipbl - skip blanks, tabs at str[i], increment i
  113.  *
  114.  */
  115. skipbl(str,i)
  116. char str[];
  117. int *i;
  118. {
  119.     int n;
  120.     dprintf("skipbl  ");
  121.     n = *i;
  122.     while (str[n] == ' ' || str[n] == '\t')
  123.         n++;
  124.     *i = n;
  125. }
  126.  
  127. /*
  128.  * itoc - special version of itoa
  129.  *
  130.  */
  131. int
  132. itoc(n,str,size)
  133. int n;
  134. char str[];
  135. int size;
  136. {
  137.  
  138.     int i,j,k,sign;
  139.     char c;
  140.  
  141.     dprintf("itoc  ");
  142.     if ((sign = n) < 0)
  143.         n = -n;
  144.     i = 0;
  145.     do {
  146.         str[i++] = n % 10 + '0'; 
  147.     } 
  148.     while ((n /= 10) > 0 && i < size-2);
  149.     if (sign < 0 && i < size-1)
  150.         str[i++] = '-';
  151.     str[i] = EOS;
  152.     /*
  153.          * reverse the string and plug it back in
  154.          *
  155.  */
  156.     for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
  157.         c = str[j];
  158.         str[j] = str[k];
  159.         str[k] = c;
  160.     }
  161.     return(i);
  162. }
  163.  
  164. /*
  165.  * usage - obvious..
  166.  *
  167.  */
  168. usage()
  169. {
  170. #ifdef rainbow
  171.     fprintf(stderr,"%s %s",
  172.     "usage: proff \033[7m[+n] [-n] [-v] [-ifile] [-s] [-pon]\033[0m",
  173.     "\033[1minfile\033[0m \033[7m[outfile]\033[0m\n");
  174. #else
  175.     fprintf(stderr,
  176.     "usage: proff [+n] [-n] [-v] [-ifile] [-s] [-pon] infile [outfile]\n");
  177. #endif
  178.     exit(0);
  179. }
  180.